<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/27 0027
 * Time: 下午 8:11
 */
namespace app\api\controller;
use app\admin\model\Tenant;
use app\common\DictConstant;

use app\api\service\WxNotify;
use app\common\validate\IDMustBePostiveInt;
use app\api\service\Pay as PayService;

use app\api\service\WxOffNotify;

class Pay extends BaseController{

    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'getPreOrder']
    ];

    //微信支付
    public function getPreOrder($id = '')
    {
        (new IDMustBePostiveInt())->goCheck();
        $pay = new PayService($id);
        return $pay->pay();
    }

    //余额支付
    public function balancePay($id = ''){
        (new IDMustBePostiveInt())->goCheck();
        $formId = '';
        if(input('post.formId')){
            $formId = input('post.formId');
        }
        $pay = new PayService($id);
        $payresult = $pay->balancepay($formId);
        if($payresult['Balance'] == DictConstant::BALANCE_TRUE && $payresult['isPay']){
            return show(config('status.SUCCESS_STATUS'),'支付成功',$payresult);
        }elseif ($payresult['Balance'] == DictConstant::BALANCE_FALSE){
            return show(config('status.ERROR_STATUS'),'账户余额不足',$payresult);
        }else{
            return show(config('status.ERROR_STATUS'),'订单异常',$payresult);
        }

    }

    //当面付-微信支付
    public function offlineWecharPay($id = '') {
        (new IDMustBePostiveInt())->goCheck();
        $pay = new PayService($id);
        return $pay->offPay();
    }

    //当面付-余额支付
    public function offlineBalancePay($id = '') {
        (new IDMustBePostiveInt())->goCheck();
        $formId = '';
        if(input('post.formId')){
            $formId = input('post.formId');
        }
        
        $pay = new PayService($id);
        $payresult = $pay->offlineBalance($formId);
        if($payresult['Balance'] == DictConstant::BALANCE_TRUE && $payresult['isPay']){
            return show(config('status.SUCCESS_STATUS'),'支付成功',$payresult);
        }elseif ($payresult['Balance'] == DictConstant::BALANCE_FALSE){
            return show(config('status.ERROR_STATUS'),'账户余额不足',$payresult);
        }else{
            return show(config('status.ERROR_STATUS'),'订单异常',$payresult);
        }

    }

    //微信支付结果回调地址
    public function receiveNotify(){
        //通知频率为15/15/30/180/1800/1800/1800/1800/3600，单位：秒
        //1.检测库存量
        //2.更新订单状态
        //3.减库存
        //如果成功处理，则向微信返回成功处理的消息

        //特点：post；xml格式；不会携带参数

        $notify = new WxNotify();
        $notify->Handle();

    }

    //微信支付结果回调地址-当面付
    public function receiveNotifyOffline(){
        $notify = new WxOffNotify();
        $notify->Handle();
    }


}